<?php

namespace App\Models\Home;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;

class Client extends Model
{
    //
    protected $table = 'client';
    protected $guarded = [];

    protected static function boot()
    {
        parent::boot();

        self::addGlobalScope("avaiable",function (Builder $query){
            $query->whereIn('status',[1]);
        });
    }

    public function user()
    {
        return $this->belongsTo('App\Models\Home\Users','uid','id');
    }

    public function assign_user()
    {
        return $this->belongsTo('App\Models\Home\Users','assign','id');
    }

    public function check_schedule()
    {
        return $this->hasOne('App\Models\Home\ClientSchedule', 'client_id', 'id');
    }

    public function one_schedule()
    {
        return $this->hasOne('App\Models\Home\ClientSchedule', 'client_id', 'id');
    }

    public function all_schedule()
    {
        return $this->hasMany('App\Models\Home\ClientSchedule', 'client_id', 'id');
    }

    public function download_log()
    {
        return $this->morphMany('App\Models\Home\DownloadLog', 'DownloadLog','type','cid');
    }

    public function SetAssign($obj){
        $obj->load(['check_schedule'=>function($query){
            $query->orderBy('id','desc');
        }]);

        if ($obj->one_schedule && $obj->one_schedule->result == 4){
            return $obj;
        }

        $assign = $obj->assign;
        $assign_time = $obj->assign_time;
        $share = $obj->share;

        if($obj->assign){
            if($obj->assign_time <= time()){
                $obj->assign = '';
                $obj->assign_time = '';
                $obj->share = 1;
                //插一条跟进记录
            }else{
                $obj->share = 0;
            }
        }else{
            $obj->share = 1;
            $obj->assign = '';
            $obj->assign_time = '';
        }

        if($assign != $obj->assign||$assign_time != $obj->assign_time||$share != $obj->share){

            $obj->save();

        }

        return $obj;
    }


    public function getList($skip,$limit,$opt=array(),$in=array(),$user=true,$assign=true,$schedule=1){

        if (!isset($limit)||!isset($skip)){
            return [];
        }
        if (!isset($skip)){
            $skip = 0;
        }

        $before = self::get();

        foreach ($before as $k => $v){

            $this->SetAssign($v);

        }

        $query = Client::orderBy('id', 'desc');

        if (!empty($opt)){
            foreach ($opt as $k => $v){
                if(is_array($v) && $v[1]){
                    $query = $query->where($k,$v[0],$v[1]);
                }elseif(!is_array($v) && $v !== false){
                    $query = $query->where($k,$v);
                }
            }
        }

        if (!empty($in) && count($in)==2){
            $query = $query->whereIn($in[0],$in[1]);
        }

        $query = $query->skip($skip)->limit($limit);

        if ($user){
            $query = $query->with(['user']);
        }

        if ($assign){
            $query = $query->with(['assign_user']);
        }

        if ($schedule == 1){
            $query = $query->with(['one_schedule'=>function($query){
                $query->orderBy('id', 'desc');
            }]);
        }elseif($schedule == 2){
            $query = $query->with(['all_schedule']);
        }

        $list = $query->get();

        foreach ($list as $k => $v){

            if ($user){
                $list[$k]->user_name = $v->user?$v->user->username:'';
            }

            if ($assign){
                $list[$k]->assign_name = $v->assign_user?$v->assign_user->username:'';
            }

            if ($schedule == 1){
                if($v->one_schedule){
                    $list[$k]->schedule_details = $v->one_schedule?$v->one_schedule->details:'';
                    $list[$k]->schedule_time = $v->one_schedule?$v->one_schedule->created_at:'';
                    $result = $v->one_schedule?$v->one_schedule->result:'';
                    switch ($result){
                        case 1:
                            $schedule_result = '待沟通';
                            break;
                        case 2:
                            $schedule_result = '有意向，跟进中';
                            break;
                        case 3:
                            $schedule_result = '未签合约';
                            break;
                        case 4:
                            $schedule_result = '已签合同';
                            break;
                        case 5:
                            $schedule_result = '不考虑';
                            break;
                        default:
                            $schedule_result = '待沟通';
                    }
                    $list[$k]->schedule_result = $schedule_result;
                }
            }
        }

        return $list;
    }

    public function getCount($opt=array(),$in=array()){

        $query = Client::orderBy('id', 'desc');

        if (!empty($opt)){
            foreach ($opt as $k => $v){
                if(is_array($v) && $v[1]){
                    $query = $query->where($k,$v[0],$v[1]);
                }elseif(!is_array($v) && $v !== false){
                    $query = $query->where($k,$v);
                }
            }
        }

        if (!empty($in) && count($in)==2){
            $query = $query->whereIn($in[0],$in[1]);
        }

        return $query->count();
    }

    public function getAll($opt=array(),$in=array()){

        $query = Client::orderBy('id', 'desc');

        if (!empty($opt)){
            foreach ($opt as $k => $v){
                if(is_array($v) && $v[1]){
                    $query = $query->where($k,$v[0],$v[1]);
                }elseif(!is_array($v) && $v !== false){
                    $query = $query->where($k,$v);
                }
            }
        }

        if (!empty($in)){
            foreach ($in as $k => $v){
                $query = $query->whereIn($k,$v);
            }
        }

        $list = $query->get();

        foreach ($list as $k => $v){

            $list[$k] = $this->SetAssign($v);

        }

        return $list;
    }
}
